package org.oimwrapper.connectors;

import java.util.*;

/**
 * Represents a request in OIM.
 */
public class OIMRequest {
	
	/**
	 * Indicates that the request is for a user.
	 */
	public final static String RequestForUser = "U";
	
	/**
	 * Indicates that the request is for an organization.
	 */
	public final static String RequestForOrg = "O";
	
	/**
	 * Stores the high level information about the request.
	 */
	protected Map requestInfo;
	
	/**
	 * Stores a list of target users/orgs for the request.
	 */
	protected Set targets;
	
	/**
	 * Stores a list of the requested objects, and the form values for each.
	 */
	protected Map objects;

	/**
	 * Default Constructor.
	 * 
	 * @param ForUserOrOrg Indicates whether the request is to be for a user or an org.
	 */
	public OIMRequest(String ForUserOrOrg) {
		requestInfo = new HashMap();
		targets = new HashSet();
		objects = new HashMap();
		requestInfo.put("Requests.Target Type", ForUserOrOrg);
		requestInfo.put("Requests.Scheduled Provisioning", "0");
		requestInfo.put("Requests.Object Request Type", "Add");
	}
	
	/**
	 * Adds a user to the request.
	 * 
	 * @param UserKey The key of the user to add.
	 * @exception Exception
	 */
	public void addTargetUser(long UserKey) throws Exception {
		if (!forUser()) {
			throw new Exception("Request is not ordered for users.");
		}
		targets.add(new Long(UserKey));
	}
	
	/**
	 * Adds an organization to the request.
	 * 
	 * @param OrgKey The key of the organization.
	 * @exception Exception
	 */
	public void addTargetOrg(long OrgKey) throws Exception {
		if (!forOrg()) {
			throw new Exception("Request is not ordered for organizations.");
		}
		targets.add(new Long(OrgKey));
	}
	
	/**
	 * Adds a resource object to the request.
	 * 
	 * @param ObjectKey The key indicating the type of resource object to request.
	 * @param Data The object form data associated with the object.
	 */
	public void addResourceObject(long ObjectKey, OIMFormData Data) {
		Long objectKey = new Long(ObjectKey);
		List objectData = (List) objects.get(objectKey);
		if (objectData == null) {
			objectData = new ArrayList();
			objects.put(objectKey, objectData);
		}
		objectData.add(Data);
	}
	
	/**
	 * Gets the request info.
	 * 
	 * @return The request info.
	 */
	public Map getRequestInfo() {
		return requestInfo;
	}
	
	/**
	 * Gets the target info.
	 * 
	 * @return The target info.
	 */
	public Set getTargets() {
		return targets;
	}
	
	/**
	 * Gets the object info.
	 * 
	 * @return The object info.
	 */
	public Map getObjects() {
		return objects;
	}
	
	/**
	 * Indicates if a request is for a user.
	 * 
	 * @return <code>true</code> if the request is for a user, <code>false</code> if not.
	 */
	public boolean forUser() {
		return ((String) requestInfo.get("Requests.Target Type")).equals(RequestForUser);
	}
	
	/**
	 * Indicates if a request is for an org.
	 * 
	 * @return <code>true</code> if the request is for an org, <code>false</code> if not.
	 */
	public boolean forOrg() {
		return ((String) requestInfo.get("Requests.Target Type")).equals(RequestForOrg);
	}
}